package com.landary.parameterrplacement.service;

import com.landary.parameterrplacement.entity.Dept;
import com.landary.parameterrplacement.utils.JDBCUtils;
import com.landary.parameterrplacement.utils.XmlToMapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 刘欣
 * @createTime 2023/8/1 15:41
 * @description 获取部门
 */
@Service
@Slf4j
public class AnalysisDeptXmlService {

    @Autowired
    private JDBCUtils jdbcUtils;

    public List<Dept> xmlToMap(String xml, String tagName) throws Exception {
        List<Map<String, String>> listMap = (List<Map<String, String>>) XmlToMapUtils.xmlToMap(xml, tagName);
        List<Dept> depts = Dept.mapToUserList(listMap);
        //查询mk组织架构数据库，获取所有部门
        List<Dept> dbDept = getDBDept();
//        List<Dept> dbDept = getDBDeptTest();
        List<Dept> changeDepts = dbDept.stream()
                .filter(dbDeptItem -> depts.stream()
                        .noneMatch(deptItem -> deptItem.getAvicDeptCode().equals(dbDeptItem.getAvicDeptCode())))
                .collect(Collectors.toList());

        //判断是否有部门变更
        if (changeDepts.size() > 0 || changeDepts != null) {
            //新建改变的部门，放入depts中
            for (Dept changeDept : changeDepts) {
                Dept dept = new Dept();
                dept.setAvicDeptCode(changeDept.getAvicDeptCode());
                dept.setIsAvailable(false);
                depts.add(dept);
            }
        }
        log.info("无效部门" + changeDepts);
        if (depts == null || depts.size() == 0) {
            throw new Exception("xml解析有误！");
        }
        return depts;
    }

    public List<Dept> getDBDept() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<Dept> depts = new ArrayList<>();

        try {
            conn = jdbcUtils.getConnection();
            stmt = conn.createStatement();

            String sql = "SELECT FD_ORI_ID FROM SYS_ORG_ELEMENT  WHERE FD_ORG_TYPE = 1";
            rs = stmt.executeQuery(sql);
            log.info("执行的sql", sql);
//            log.info("SQL执行结果集", rs.toString());
            while (rs.next()) {
                String fdid = rs.getString("FD_ORI_ID");
                if (fdid != null && fdid.contains("dept:")) {
                    Dept dept = new Dept();
                    String res = fdid.replace("dept:", "");
                    dept.setAvicDeptCode(res);
                    depts.add(dept);
                }
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
            log.info("SQL语句执行异常", e);
        } finally {
            JDBCUtils.close(conn, stmt, rs);
        }
        return depts;
    }

    public List<Dept> getDBDeptTest() {
        List<Dept> depts = new ArrayList<>();
        Dept dept1 = new Dept();
        dept1.setAvicDeptCode("101");
        depts.add(dept1);
        Dept dept2 = new Dept();
        dept2.setAvicDeptCode("111");
        depts.add(dept2);
        return depts;
    }
}
